<?php

namespace App\Utility;

use EasySwoole\Component\Singleton;
use EasySwoole\Http\Request;
use EasySwoole\Pay\AliPay\Config;
use EasySwoole\Pay\AliPay\GateWay;
use EasySwoole\Pay\Exceptions\GatewayException;
use EasySwoole\Pay\Exceptions\InvalidArgumentException;
use EasySwoole\Pay\Exceptions\InvalidConfigException;
use EasySwoole\Pay\Exceptions\InvalidSignException;
use EasySwoole\Pay\Pay;
use EasySwoole\Pay\WeChat\Utility;
use EasySwoole\Pay\WeChat\WeChatPay\App;
use EasySwoole\Spl\SplArray;

class Payment
{
    use Singleton;

    /**
     * @throws InvalidConfigException
     */
    function Alipay($appid, $pubkey, $prikey, $rootkey, $apubkey, $amount, $name, $order_no, $notifyUrl = null): string
    {
        // 由于单笔转账必须使用公钥证书签名，故只有公钥证书的单测
        $aliConfig = new Config();
        $aliConfig->setCertMode(true);
        $aliConfig->setGateWay(GateWay::NORMAL);
        $aliConfig->setAppId($appid);
        $aliConfig->setPrivateKey($prikey);
        $aliConfig->setCertPath(EASYSWOOLE_ROOT . $pubkey);
        $aliConfig->setRootCertPath(EASYSWOOLE_ROOT . $rootkey);
        $aliConfig->setMerchantCertPath(EASYSWOOLE_ROOT . $apubkey);
        $aliConfig->setSignType('RSA2');
        $aliConfig->setCharset("GBK");
        $aliConfig->setFormat("json");
        $aliConfig->setNotifyUrl($notifyUrl);
        $pay = new Pay();
        $order = new \EasySwoole\Pay\AliPay\RequestBean\App();
        $order->setSubject($name);
        $order->setOutTradeNo($order_no);
        $order->setTotalAmount($amount);
        $order->setNotifyUrl($notifyUrl);
        $res = $pay->aliPay($aliConfig)->app($order);
// 前端需要的支付信息
        return http_build_query($res->toArray());
    }

    /**
     * @param $appid
     * @param $public
     * @param $prikey
     * @param $rootkey
     * @param $apubkey
     * @param $remark
     * @param $amount
     * @param $account
     * @param $name
     * @return SplArray
     * @throws GatewayException
     * @throws InvalidConfigException
     * @throws InvalidSignException
     */
    public function apiPayment($appid, $public, $prikey, $rootkey, $apubkey, $remark, $amount, $account, $name): SplArray
    {
        // 由于单笔转账必须使用公钥证书签名，故只有公钥证书的单测
        $aliConfig = new Config();
        $aliConfig->setCertMode(true);
        $aliConfig->setGateWay(GateWay::NORMAL);
        $aliConfig->setAppId($appid);
        $aliConfig->setPrivateKey($prikey);
        $aliConfig->setCertPath(EASYSWOOLE_ROOT . $public);
        $aliConfig->setRootCertPath(EASYSWOOLE_ROOT . $rootkey);
        $aliConfig->setMerchantCertPath(EASYSWOOLE_ROOT . $apubkey);
        $pay = new Pay();
        $order = new \EasySwoole\Pay\AliPay\RequestBean\Transfer();
        $outBizNo = time();
        $order->setOutBizNo($outBizNo);
        $order->setTransAmount($amount);
        $order->setProductCode('TRANS_ACCOUNT_NO_PWD');
        $order->setBizScene('DIRECT_TRANSFER');
        $order->setOrderTitle($remark);
        $order->setPayeeInfo([
            'identity' => $account,
            'identity_type' => 'ALIPAY_LOGON_ID',
            'name' => $name
        ]);
        $order->setRemark('【优等生活】用户单笔提现');
        $aliPay = $pay->aliPay($aliConfig);
        $data = $aliPay->transfer($order)->toArray();
        return $aliPay->preQuest($data);
    }

    /**
     * @param $appId
     * @param $miniAppId
     * @param $mchId
     * @param $password
     * @param $cert
     * @param $key
     * @param $order_no
     * @param $amount
     * @param $name
     * @param $notifyUrl
     * @param $ip
     * @return \EasySwoole\Pay\WeChat\ResponseBean\App
     */
    function wepay($appId, $miniAppId, $mchId, $password, $cert, $key, $order_no, $amount, $name, $notifyUrl, $ip): \EasySwoole\Pay\WeChat\ResponseBean\App
    {
        $wechatConfig = new \EasySwoole\Pay\WeChat\Config();
        $wechatConfig->setAppId($appId);      // 除了小程序以外使用该APPID
        $wechatConfig->setMiniAppId($miniAppId);  // 小程序使用该APPID
        $wechatConfig->setMchId($mchId);
        $wechatConfig->setKey($password);
        $wechatConfig->setNotifyUrl($notifyUrl);
        $wechatConfig->setApiClientCert(EASYSWOOLE_ROOT . $cert);//客户端证书
        $wechatConfig->setApiClientKey(EASYSWOOLE_ROOT . $key); //客户端证书秘钥
        var_dump($wechatConfig);
        $app = new \EasySwoole\Pay\WeChat\RequestBean\App();
        var_dump($app);
        $app->setNonceStr($order_no);
        //$app->setSign('WXPay');
        $app->setBody($name . '-' . $order_no);
        $app->setOutTradeNo($order_no);
        $app->setTotalFee(intval($amount));
        $app->setSpbillCreateIp($ip);

        $pay = new \EasySwoole\Pay\Pay();
        $result = $pay->weChat($wechatConfig)->app($app);
        var_dump($result);
        return $result;
    }

    function mxWepay($appId, $miniAppId, $mchId, $password, $cert, $key, $openId, $order_no, $amount, $name, $notifyUrl, $ip): \EasySwoole\Pay\WeChat\ResponseBean\MiniProgram
    {
        $wechatConfig = new \EasySwoole\Pay\WeChat\Config();
        $wechatConfig->setAppId($appId);      // 除了小程序以外使用该APPID
        $wechatConfig->setMiniAppId($miniAppId);  // 小程序使用该APPID
        $wechatConfig->setMchId($mchId);
        $wechatConfig->setKey($password);
        $wechatConfig->setNotifyUrl($notifyUrl);
        $wechatConfig->setApiClientCert(EASYSWOOLE_ROOT . $cert);//客户端证书
        $wechatConfig->setApiClientKey(EASYSWOOLE_ROOT . $key); //客户端证书秘钥

        $bean = new \EasySwoole\Pay\WeChat\RequestBean\MiniProgram();
        $bean->setOpenid($openId);
        $bean->setOutTradeNo($order_no);
        $bean->setBody($name . '-' . $order_no);
        $bean->setTotalFee(intval($amount));
        $bean->setSpbillCreateIp($ip);

        $pay = new \EasySwoole\Pay\Pay();
        return $pay->weChat($wechatConfig)->miniProgram($bean);
    }

    /**
     * @param $appId
     * @param $miniAppId
     * @param $mchId
     * @param $password
     * @param $notifyUrl
     * @param $param
     * @return SplArray
     * @throws InvalidArgumentException
     * @throws InvalidSignException
     */
    function mxWepayCallback($appId, $miniAppId, $mchId, $password, $notifyUrl, $param): SplArray
    {
        $wechatConfig = new \EasySwoole\Pay\WeChat\Config();
        $wechatConfig->setAppId($appId);      // 除了小程序以外使用该APPID
        $wechatConfig->setMiniAppId($miniAppId);  // 小程序使用该APPID
        $wechatConfig->setMchId($mchId);
        $wechatConfig->setKey($password);
        $wechatConfig->setNotifyUrl($notifyUrl);
        $wechatConfig->setApiClientCert(EASYSWOOLE_ROOT . '/Client/apiclient_cert.pem');//客户端证书
        $wechatConfig->setApiClientKey(EASYSWOOLE_ROOT . '/Client/apiclient_key.pem'); //客户端证书秘钥

        $pay = new \EasySwoole\Pay\Pay();
        $content = $param;//content为xml原始数据,在asystole中可通过$this->request()->getBody()->__toString()取出
        return $pay->weChat($wechatConfig)->verify($content);
    }
}